package cn.liujianbo.easycode.biz;


import cn.liujianbo.easycode.common.I18nBusinessException;
import cn.liujianbo.easycode.common.PageVO;
import cn.liujianbo.easycode.common.enums.ErrorEnum;
import cn.liujianbo.easycode.entity.BaseInfo;
import cn.liujianbo.easycode.model.req.BaseInfoInsertReq;
import cn.liujianbo.easycode.model.req.BaseInfoPageReq;
import cn.liujianbo.easycode.model.req.BaseInfoUpdateReq;
import cn.liujianbo.easycode.model.vo.BaseInfoVO;
import cn.liujianbo.easycode.service.BaseInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 网站基础信息(BaseInfo)业务服务
 *
 * @author 刘建波
 * @since 2022-06-10 14:24:16
 */
@Slf4j
@RequiredArgsConstructor
@Service("baseInfoBiz")
public class BaseInfoBiz {
    private final BaseInfoService baseInfoService;

    public void del(Integer id) {
        baseInfoService.removeById(id);
    }

    public PageVO<BaseInfoVO> page(BaseInfoPageReq req) {
        BaseInfo entity = new BaseInfo();
        BeanUtils.copyProperties(req, entity);
        Page<BaseInfo> page = baseInfoService.page(Page.of(req.getPageNum(), req.getPageSize()), new QueryWrapper<>(entity));
        return PageVO.of(page, BaseInfoVO.class);
    }

    public void add(BaseInfoInsertReq req) {
        BaseInfo entity = new BaseInfo();
        BeanUtils.copyProperties(req, entity);
        baseInfoService.save(entity);
    }

    public void updateById(BaseInfoUpdateReq req) {
        BaseInfo entity = baseInfoService.getById(req.getId());
        if (Objects.isNull(entity)) {
            throw new I18nBusinessException(ErrorEnum.NO_DATA_IS_FOUND, "id = " + req.getId());
        }

        entity.setUpdateTime(null);
        BeanUtils.copyProperties(req, entity);
        baseInfoService.updateById(entity);
    }

    public BaseInfoVO info(Integer id) {
        BaseInfo entity = baseInfoService.getById(id);
        if (Objects.isNull(entity)) {
            return null;
        }

        BaseInfoVO vo = new BaseInfoVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}

